package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.dao.hibernate.search.SearchCondition;
import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.ProductBatch;
import com.eling.elcms.travel.service.IProductBatchManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class ProductBatchController extends BaseFormController{

	@Autowired
	private IProductBatchManager batchManager;
	
	@RequestMapping("/api/productbatch/save")
    @ResponseBody
	public ProductBatch save(@ModelAttribute@ModelFilling ProductBatch productBatch){
		ProductBatch productbatch = new ProductBatch();
		String name = productBatch.getName();
		productbatch.setName(name);
		if(productBatch.getPkProductBatch()==null){
			List<ProductBatch> productBatchs = batchManager.query(productbatch);
			//判断名称是否已经存在
			for(int i =0; i < productBatchs.size();i++){
				if(productBatchs.get(i).getName().equals(productBatch.getName())){
					throw new BusinessException("名称已存在");
				}
			}
		}
		return batchManager.save(productBatch);
	}
	
	@RequestMapping("/api/productbatch/{pkProductBatch}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkProductBatch){
		batchManager.remove(pkProductBatch);
		return new Msg("删除成功");
	}
	
	@RequestMapping("/api/productbatch/query")
    @ResponseBody
	public List<ProductBatch> query(@ModelAttribute ProductBatch productBatch){
		return batchManager.query(productBatch);
	}
	
	@RequestMapping("/api/productbatch/search*")
	@ResponseBody
	public List<ProductBatch> search(@ModelAttribute SearchCondition sc,@ModelAttribute ProductBatch productBatch){
		sc.setQueryCondition(productBatch);
		return batchManager.search(sc);
	}
	
	@RequestMapping("/api/productbatch/changeseal")
	@ResponseBody
	public ProductBatch changeseal(@ModelAttribute ProductBatch productBatch){
		return batchManager.changeseal(productBatch);
	}
	
	@RequestMapping("/api/productbatch/savebatch")
    @ResponseBody
	public ProductBatch savebatch(@ModelAttribute@ModelFilling ProductBatch productBatch){
		return batchManager.savebatch(productBatch);
	}//自定义保存方法
	
	@RequestMapping("/api/productbatch/seekquote")
    @ResponseBody
	public boolean seekquote(@RequestParam(value ="pkProductBatch",required = false) Long pkProductBatch){
		return batchManager.seekquote(pkProductBatch);
	}//引用状态查询
}
